<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>数据库操作 | Python Web 框架要点</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Database/QuerySet.html" />
    
    
    <link rel="prev" href="../C05-Database/Tools.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.4"
        data-chapter-title="数据库操作"
        data-filepath="C05-Database/CURD.md"
        data-basepath=".."
        data-revision="Sat May 05 2018 07:17:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C02-SettingsStaticAndURL/AppConfig.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/AppConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        App应用配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Database/index.html">
            
                
                    <a href="../C05-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Database/Config.html">
            
                
                    <a href="../C05-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Database/Model.html">
            
                
                    <a href="../C05-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Database/Tools.html">
            
                
                    <a href="../C05-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.4" data-path="C05-Database/CURD.html">
            
                
                    <a href="../C05-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C05-Database/QuerySet.html">
            
                
                    <a href="../C05-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C05-Database/Manager.html">
            
                
                    <a href="../C05-Database/Manager.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        管理器Manager
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Template/index.html">
            
                
                    <a href="../C06-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Template/template.html">
            
                
                    <a href="../C06-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Form/index.html">
            
                
                    <a href="../C07-Form/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        表单
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Form/form.html">
            
                
                    <a href="../C07-Form/form.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        表单使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C08-Admin/index.html">
            
                
                    <a href="../C08-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C08-Admin/Config.html">
            
                
                    <a href="../C08-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C08-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C08-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C08-Admin/ModifySite.html">
            
                
                    <a href="../C08-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C08-Admin/UploadImage.html">
            
                
                    <a href="../C08-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python Web 框架要点</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#x2014;&#x589E;&#x3001;&#x5220;&#x3001;&#x6539;&#x3001;&#x67E5;">&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#x2014;&#x589E;&#x3001;&#x5220;&#x3001;&#x6539;&#x3001;&#x67E5;</h1>
<h2 id="1-&#x589E;&#x52A0;">1 &#x589E;&#x52A0;</h2>
<p>&#x589E;&#x52A0;&#x6570;&#x636E;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#x3002;</p>
<p><strong>1&#xFF09;save</strong></p>
<p>&#x901A;&#x8FC7;&#x521B;&#x5EFA;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;&#xFF0C;&#x6267;&#x884C;&#x5BF9;&#x8C61;&#x7684;save()&#x65B9;&#x6CD5;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> date
<span class="hljs-prompt">&gt;&gt;&gt; </span>book = BookInfo(
    btitle=<span class="hljs-string">&apos;&#x897F;&#x6E38;&#x8BB0;&apos;</span>,
    bput_date=date(<span class="hljs-number">1988</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>),
    bread=<span class="hljs-number">10</span>,
    bcomment=<span class="hljs-number">10</span>
)
<span class="hljs-prompt">&gt;&gt;&gt; </span>book.save()
<span class="hljs-prompt">&gt;&gt;&gt; </span>hero = HeroInfo(
    hname=<span class="hljs-string">&apos;&#x5B59;&#x609F;&#x7A7A;&apos;</span>,
    hgender=<span class="hljs-number">0</span>,
    hbook=book
)
<span class="hljs-prompt">&gt;&gt;&gt; </span>hero.save()
<span class="hljs-prompt">&gt;&gt;&gt; </span>hero2 = HeroInfo(
    hname=<span class="hljs-string">&apos;&#x732A;&#x516B;&#x6212;&apos;</span>,
    hgender=<span class="hljs-number">0</span>,
    hbook_id=book.id
)
<span class="hljs-prompt">&gt;&gt;&gt; </span>hero2.save()
</code></pre>
<p><strong>2&#xFF09;create</strong></p>
<p>&#x901A;&#x8FC7;&#x6A21;&#x578B;&#x7C7B;.objects.create()&#x4FDD;&#x5B58;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>HeroInfo.objects.create(
    hname=<span class="hljs-string">&apos;&#x6C99;&#x609F;&#x51C0;&apos;</span>,
    hgender=<span class="hljs-number">0</span>,
    hbook=book
)
&lt;HeroInfo: &#x6C99;&#x609F;&#x51C0;&gt;
</code></pre>
<h2 id="2-&#x67E5;&#x8BE2;">2 &#x67E5;&#x8BE2;</h2>
<h3 id="21-&#x57FA;&#x672C;&#x67E5;&#x8BE2;">2.1 &#x57FA;&#x672C;&#x67E5;&#x8BE2;</h3>
<p><strong>get</strong> &#x67E5;&#x8BE2;&#x5355;&#x4E00;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x4F1A;&#x629B;&#x51FA;<strong>&#x6A21;&#x578B;&#x7C7B;.DoesNotExist</strong>&#x5F02;&#x5E38;&#x3002;</p>
<p><strong>all </strong>&#x67E5;&#x8BE2;&#x591A;&#x4E2A;&#x7ED3;&#x679C;&#x3002;</p>
<p><strong>count</strong> &#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6570;&#x91CF;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>BookInfo.objects.all()
&lt;QuerySet [&lt;BookInfo: &#x5C04;&#x96D5;&#x82F1;&#x96C4;&#x4F20;&gt;, &lt;BookInfo: &#x5929;&#x9F99;&#x516B;&#x90E8;&gt;, &lt;BookInfo: &#x7B11;&#x50B2;&#x6C5F;&#x6E56;&gt;, &lt;BookInfo: &#x96EA;&#x5C71;&#x98DE;&#x72D0;&gt;, &lt;BookInfo: &#x897F;&#x6E38;&#x8BB0;&gt;]&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span>book = BookInfo.objects.get(btitle=<span class="hljs-string">&apos;&#x897F;&#x6E38;&#x8BB0;&apos;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>book.id
<span class="hljs-number">5</span>

<span class="hljs-prompt">&gt;&gt;&gt; </span>BookInfo.objects.get(id=<span class="hljs-number">3</span>)
&lt;BookInfo: &#x7B11;&#x50B2;&#x6C5F;&#x6E56;&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span>BookInfo.objects.get(pk=<span class="hljs-number">3</span>)
&lt;BookInfo: &#x96EA;&#x5C71;&#x98DE;&#x72D0;&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span>BookInfo.objects.get(id=<span class="hljs-number">100</span>)
Traceback (most recent call last):
  File <span class="hljs-string">&quot;&lt;console&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
  File <span class="hljs-string">&quot;/Users/delron/.virtualenv/dj/lib/python3.6/site-packages/django/db/models/manager.py&quot;</span>, line <span class="hljs-number">85</span>, <span class="hljs-keyword">in</span> manager_method
    <span class="hljs-keyword">return</span> getattr(self.get_queryset(), name)(*args, **kwargs)
  File <span class="hljs-string">&quot;/Users/delron/.virtualenv/dj/lib/python3.6/site-packages/django/db/models/query.py&quot;</span>, line <span class="hljs-number">380</span>, <span class="hljs-keyword">in</span> get
    self.model._meta.object_name
db.models.DoesNotExist: BookInfo matching query does <span class="hljs-keyword">not</span> exist.

<span class="hljs-prompt">&gt;&gt;&gt; </span>BookInfo.objects.count()
<span class="hljs-number">6</span>
</code></pre>
<h3 id="22-&#x8FC7;&#x6EE4;&#x67E5;&#x8BE2;">2.2 &#x8FC7;&#x6EE4;&#x67E5;&#x8BE2;</h3>
<p>&#x5B9E;&#x73B0;SQL&#x4E2D;&#x7684;where&#x529F;&#x80FD;&#xFF0C;&#x5305;&#x62EC;</p>
<ul>
<li><strong>filter</strong>  &#x8FC7;&#x6EE4;&#x51FA;&#x591A;&#x4E2A;&#x7ED3;&#x679C;</li>
<li><strong>exclude</strong> &#x6392;&#x9664;&#x6389;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x5269;&#x4E0B;&#x7684;&#x7ED3;&#x679C;</li>
<li><strong>get</strong>  &#x8FC7;&#x6EE4;&#x5355;&#x4E00;&#x7ED3;&#x679C;</li>
</ul>
<p>&#x5BF9;&#x4E8E;&#x8FC7;&#x6EE4;&#x6761;&#x4EF6;&#x7684;&#x4F7F;&#x7528;&#xFF0C;&#x4E0A;&#x8FF0;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x76F8;&#x540C;&#xFF0C;&#x6545;&#x4EC5;&#x4EE5;<strong>filter</strong>&#x8FDB;&#x884C;&#x8BB2;&#x89E3;&#x3002;</p>
<p>&#x8FC7;&#x6EE4;&#x6761;&#x4EF6;&#x7684;&#x8868;&#x8FBE;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5C5E;&#x6027;&#x540D;&#x79F0;__&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;=&#x503C;
<span class="hljs-comment"># &#x5C5E;&#x6027;&#x540D;&#x79F0;&#x548C;&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;&#x95F4;&#x4F7F;&#x7528;&#x4E24;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;&#xFF0C;&#x6240;&#x4EE5;&#x5C5E;&#x6027;&#x540D;&#x4E0D;&#x80FD;&#x5305;&#x62EC;&#x591A;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;</span>
</code></pre>
<p><strong>1&#xFF09;&#x76F8;&#x7B49;</strong></p>
<p><strong>exact&#xFF1A;&#x8868;&#x793A;&#x5224;&#x7B49;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x7F16;&#x53F7;&#x4E3A;1&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code>BookInfo.objects.filter(id__exact=1)
&#x53EF;&#x7B80;&#x5199;&#x4E3A;&#xFF1A;
BookInfo.objects.filter(id=1)
</code></pre><p><strong>2&#xFF09;&#x6A21;&#x7CCA;&#x67E5;&#x8BE2;</strong></p>
<p><strong>contains&#xFF1A;&#x662F;&#x5426;&#x5305;&#x542B;&#x3002;</strong></p>
<blockquote>
<p>&#x8BF4;&#x660E;&#xFF1A;&#x5982;&#x679C;&#x8981;&#x5305;&#x542B;%&#x65E0;&#x9700;&#x8F6C;&#x4E49;&#xFF0C;&#x76F4;&#x63A5;&#x5199;&#x5373;&#x53EF;&#x3002;</p>
</blockquote>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x4E66;&#x540D;&#x5305;&#x542B;&apos;&#x4F20;&apos;&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(btitle__contains=<span class="hljs-string">&apos;&#x4F20;&apos;</span>)
</code></pre>
<p><strong>startswith&#x3001;endswith&#xFF1A;&#x4EE5;&#x6307;&#x5B9A;&#x503C;&#x5F00;&#x5934;&#x6216;&#x7ED3;&#x5C3E;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x4E66;&#x540D;&#x4EE5;&apos;&#x90E8;&apos;&#x7ED3;&#x5C3E;&#x7684;&#x56FE;&#x4E66;</p>
<pre><code class="lang-python">BookInfo.objects.filter(btitle__endswith=<span class="hljs-string">&apos;&#x90E8;&apos;</span>)
</code></pre>
<blockquote>
<p>&#x4EE5;&#x4E0A;&#x8FD0;&#x7B97;&#x7B26;&#x90FD;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#xFF0C;&#x5728;&#x8FD9;&#x4E9B;&#x8FD0;&#x7B97;&#x7B26;&#x524D;&#x52A0;&#x4E0A;i&#x8868;&#x793A;&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#xFF0C;&#x5982;iexact&#x3001;icontains&#x3001;istartswith&#x3001;iendswith.</p>
</blockquote>
<p><strong>3&#xFF09; &#x7A7A;&#x67E5;&#x8BE2;</strong></p>
<p><strong>isnull&#xFF1A;&#x662F;&#x5426;&#x4E3A;null&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x4E66;&#x540D;&#x4E0D;&#x4E3A;&#x7A7A;&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(btitle__isnull=<span class="hljs-keyword">False</span>)
</code></pre>
<p><strong>4&#xFF09; &#x8303;&#x56F4;&#x67E5;&#x8BE2;</strong></p>
<p><strong>in&#xFF1A;&#x662F;&#x5426;&#x5305;&#x542B;&#x5728;&#x8303;&#x56F4;&#x5185;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x7F16;&#x53F7;&#x4E3A;1&#x6216;3&#x6216;5&#x7684;&#x56FE;&#x4E66;</p>
<pre><code class="lang-python">BookInfo.objects.filter(id__in=[<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>])
</code></pre>
<p><strong>5&#xFF09;&#x6BD4;&#x8F83;&#x67E5;&#x8BE2;</strong></p>
<ul>
<li><strong>gt</strong>  &#x5927;&#x4E8E; (greater then)</li>
<li><strong>gte</strong> &#x5927;&#x4E8E;&#x7B49;&#x4E8E; (greater then equal)</li>
<li><strong>lt</strong> &#x5C0F;&#x4E8E; (less then)</li>
<li><strong>lte</strong> &#x5C0F;&#x4E8E;&#x7B49;&#x4E8E; (less then equal)</li>
</ul>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x7F16;&#x53F7;&#x5927;&#x4E8E;3&#x7684;&#x56FE;&#x4E66;</p>
<pre><code class="lang-python">BookInfo.objects.filter(id__gt=<span class="hljs-number">3</span>)
</code></pre>
<p><strong>&#x4E0D;&#x7B49;&#x4E8E;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x4F7F;&#x7528;exclude()&#x8FC7;&#x6EE4;&#x5668;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x7F16;&#x53F7;&#x4E0D;&#x7B49;&#x4E8E;3&#x7684;&#x56FE;&#x4E66;</p>
<pre><code class="lang-python">BookInfo.objects.exclude(id=<span class="hljs-number">3</span>)
</code></pre>
<p><strong>6&#xFF09;&#x65E5;&#x671F;&#x67E5;&#x8BE2;</strong></p>
<p><strong>year&#x3001;month&#x3001;day&#x3001;week_day&#x3001;hour&#x3001;minute&#x3001;second&#xFF1A;&#x5BF9;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x7C7B;&#x578B;&#x7684;&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x8FD0;&#x7B97;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;1980&#x5E74;&#x53D1;&#x8868;&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(bpub_date__year=<span class="hljs-number">1980</span>)
</code></pre>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;1980&#x5E74;1&#x6708;1&#x65E5;&#x540E;&#x53D1;&#x8868;&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(bpub_date__gt=date(<span class="hljs-number">1990</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>))
</code></pre>
<h4 id="f&#x5BF9;&#x8C61;">F&#x5BF9;&#x8C61;</h4>
<p>&#x4E4B;&#x524D;&#x7684;&#x67E5;&#x8BE2;&#x90FD;&#x662F;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4E0E;&#x5E38;&#x91CF;&#x503C;&#x6BD4;&#x8F83;&#xFF0C;&#x4E24;&#x4E2A;&#x5C5E;&#x6027;&#x600E;&#x4E48;&#x6BD4;&#x8F83;&#x5462;&#xFF1F; &#x7B54;&#xFF1A;&#x4F7F;&#x7528;F&#x5BF9;&#x8C61;&#xFF0C;&#x88AB;&#x5B9A;&#x4E49;&#x5728;django.db.models&#x4E2D;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>F(&#x5C5E;&#x6027;&#x540D;)
</code></pre><p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x9605;&#x8BFB;&#x91CF;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;&#x8BC4;&#x8BBA;&#x91CF;&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db.models <span class="hljs-keyword">import</span> F

BookInfo.objects.filter(bread__gte=F(<span class="hljs-string">&apos;bcomment&apos;</span>))
</code></pre>
<p>&#x53EF;&#x4EE5;&#x5728;F&#x5BF9;&#x8C61;&#x4E0A;&#x4F7F;&#x7528;&#x7B97;&#x6570;&#x8FD0;&#x7B97;&#x3002;</p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x9605;&#x8BFB;&#x91CF;&#x5927;&#x4E8E;2&#x500D;&#x8BC4;&#x8BBA;&#x91CF;&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(bread__gt=F(<span class="hljs-string">&apos;bcomment&apos;</span>) * <span class="hljs-number">2</span>)
</code></pre>
<h4 id="q&#x5BF9;&#x8C61;">Q&#x5BF9;&#x8C61;</h4>
<p><strong>&#x591A;&#x4E2A;&#x8FC7;&#x6EE4;&#x5668;&#x9010;&#x4E2A;&#x8C03;&#x7528;&#x8868;&#x793A;&#x903B;&#x8F91;&#x4E0E;&#x5173;&#x7CFB;&#xFF0C;&#x540C;sql&#x8BED;&#x53E5;&#x4E2D;where&#x90E8;&#x5206;&#x7684;and&#x5173;&#x952E;&#x5B57;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x9605;&#x8BFB;&#x91CF;&#x5927;&#x4E8E;20&#xFF0C;&#x5E76;&#x4E14;&#x7F16;&#x53F7;&#x5C0F;&#x4E8E;3&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(bread__gt=<span class="hljs-number">20</span>,id__lt=<span class="hljs-number">3</span>)
&#x6216;
BookInfo.objects.filter(bread__gt=<span class="hljs-number">20</span>).filter(id__lt=<span class="hljs-number">3</span>)
</code></pre>
<p><strong>&#x5982;&#x679C;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x903B;&#x8F91;&#x6216;or&#x7684;&#x67E5;&#x8BE2;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;Q()&#x5BF9;&#x8C61;&#x7ED3;&#x5408;|&#x8FD0;&#x7B97;&#x7B26;</strong>&#xFF0C;Q&#x5BF9;&#x8C61;&#x88AB;&#x4E49;&#x5728;django.db.models&#x4E2D;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>Q(&#x5C5E;&#x6027;&#x540D;__&#x8FD0;&#x7B97;&#x7B26;=&#x503C;)
</code></pre><p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x9605;&#x8BFB;&#x91CF;&#x5927;&#x4E8E;20&#x7684;&#x56FE;&#x4E66;&#xFF0C;&#x6539;&#x5199;&#x4E3A;Q&#x5BF9;&#x8C61;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db.models <span class="hljs-keyword">import</span> Q

BookInfo.objects.filter(Q(bread__gt=<span class="hljs-number">20</span>))
</code></pre>
<p>Q&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&amp;&#x3001;|&#x8FDE;&#x63A5;&#xFF0C;&amp;&#x8868;&#x793A;&#x903B;&#x8F91;&#x4E0E;&#xFF0C;|&#x8868;&#x793A;&#x903B;&#x8F91;&#x6216;&#x3002;</p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x9605;&#x8BFB;&#x91CF;&#x5927;&#x4E8E;20&#xFF0C;&#x6216;&#x7F16;&#x53F7;&#x5C0F;&#x4E8E;3&#x7684;&#x56FE;&#x4E66;&#xFF0C;&#x53EA;&#x80FD;&#x4F7F;&#x7528;Q&#x5BF9;&#x8C61;&#x5B9E;&#x73B0;</p>
<pre><code class="lang-python">BookInfo.objects.filter(Q(bread__gt=<span class="hljs-number">20</span>) | Q(pk__lt=<span class="hljs-number">3</span>))
</code></pre>
<p>Q&#x5BF9;&#x8C61;&#x524D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;~&#x64CD;&#x4F5C;&#x7B26;&#xFF0C;&#x8868;&#x793A;&#x975E;not&#x3002;</p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x7F16;&#x53F7;&#x4E0D;&#x7B49;&#x4E8E;3&#x7684;&#x56FE;&#x4E66;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.filter(~Q(pk=<span class="hljs-number">3</span>))
</code></pre>
<h4 id="&#x805A;&#x5408;&#x51FD;&#x6570;">&#x805A;&#x5408;&#x51FD;&#x6570;</h4>
<p>&#x4F7F;&#x7528;aggregate()&#x8FC7;&#x6EE4;&#x5668;&#x8C03;&#x7528;&#x805A;&#x5408;&#x51FD;&#x6570;&#x3002;&#x805A;&#x5408;&#x51FD;&#x6570;&#x5305;&#x62EC;&#xFF1A;<strong>Avg</strong> &#x5E73;&#x5747;&#xFF0C;<strong>Count</strong> &#x6570;&#x91CF;&#xFF0C;<strong>Max</strong> &#x6700;&#x5927;&#xFF0C;<strong>Min</strong> &#x6700;&#x5C0F;&#xFF0C;<strong>Sum</strong> &#x6C42;&#x548C;&#xFF0C;&#x88AB;&#x5B9A;&#x4E49;&#x5728;django.db.models&#x4E2D;&#x3002;</p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x56FE;&#x4E66;&#x7684;&#x603B;&#x9605;&#x8BFB;&#x91CF;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db.models <span class="hljs-keyword">import</span> Sum

BookInfo.objects.aggregate(Sum(<span class="hljs-string">&apos;bread&apos;</span>))
</code></pre>
<p>&#x6CE8;&#x610F;aggregate&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#xFF0C;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">  {<span class="hljs-string">&apos;&#x5C5E;&#x6027;&#x540D;__&#x805A;&#x5408;&#x7C7B;&#x5C0F;&#x5199;&apos;</span>:&#x503C;}
  &#x5982;:{<span class="hljs-string">&apos;bread__sum&apos;</span>:<span class="hljs-number">3</span>}
</code></pre>
<p>&#x4F7F;&#x7528;count&#x65F6;&#x4E00;&#x822C;&#x4E0D;&#x4F7F;&#x7528;aggregate()&#x8FC7;&#x6EE4;&#x5668;&#x3002;</p>
<p>&#x4F8B;&#xFF1A;&#x67E5;&#x8BE2;&#x56FE;&#x4E66;&#x603B;&#x6570;&#x3002;</p>
<pre><code class="lang-python">BookInfo.objects.count()
</code></pre>
<p>&#x6CE8;&#x610F;count&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x3002;</p>
<h3 id="23--&#x6392;&#x5E8F;">2.3  &#x6392;&#x5E8F;</h3>
<p>&#x4F7F;&#x7528;<strong>order_by</strong>&#x5BF9;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x6392;&#x5E8F;</p>
<pre><code class="lang-python">BookInfo.objects.all().order_by(<span class="hljs-string">&apos;bread&apos;</span>)  <span class="hljs-comment"># &#x5347;&#x5E8F;</span>
BookInfo.objects.all().order_by(<span class="hljs-string">&apos;-bread&apos;</span>)  <span class="hljs-comment"># &#x964D;&#x5E8F;</span>
</code></pre>
<h3 id="24-&#x5173;&#x8054;&#x67E5;&#x8BE2;">2.4 &#x5173;&#x8054;&#x67E5;&#x8BE2;</h3>
<p>&#x7531;&#x4E00;&#x5230;&#x591A;&#x7684;&#x8BBF;&#x95EE;&#x8BED;&#x6CD5;&#xFF1A;</p>
<p>&#x4E00;&#x5BF9;&#x5E94;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;.&#x591A;&#x5BF9;&#x5E94;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x540D;&#x5C0F;&#x5199;_set
&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python">b = BookInfo.objects.get(id=<span class="hljs-number">1</span>)
b.heroinfo_set.all()
</code></pre>
<p>&#x7531;&#x591A;&#x5230;&#x4E00;&#x7684;&#x8BBF;&#x95EE;&#x8BED;&#x6CD5;:</p>
<p>&#x591A;&#x5BF9;&#x5E94;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;.&#x591A;&#x5BF9;&#x5E94;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x4E2D;&#x7684;&#x5173;&#x7CFB;&#x7C7B;&#x5C5E;&#x6027;&#x540D;
&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python">h = HeroInfo.objects.get(id=<span class="hljs-number">1</span>)
h.hbook
</code></pre>
<p>&#x8BBF;&#x95EE;&#x4E00;&#x5BF9;&#x5E94;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;id&#x8BED;&#x6CD5;:</p>
<p>&#x591A;&#x5BF9;&#x5E94;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;.&#x5173;&#x8054;&#x7C7B;&#x5C5E;&#x6027;_id</p>
<p>&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python">h = HeroInfo.objects.get(id=<span class="hljs-number">1</span>)
h.book_id
</code></pre>
<h4 id="&#x5173;&#x8054;&#x8FC7;&#x6EE4;&#x67E5;&#x8BE2;">&#x5173;&#x8054;&#x8FC7;&#x6EE4;&#x67E5;&#x8BE2;</h4>
<p><strong>&#x7531;&#x591A;&#x6A21;&#x578B;&#x7C7B;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x4E00;&#x6A21;&#x578B;&#x7C7B;&#x6570;&#x636E;</strong>:</p>
<p>&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5173;&#x8054;&#x6A21;&#x578B;&#x7C7B;&#x540D;&#x5C0F;&#x5199;__&#x5C5E;&#x6027;&#x540D;__&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;=&#x503C;
</code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x6CA1;&#x6709;&quot;__&#x8FD0;&#x7B97;&#x7B26;&quot;&#x90E8;&#x5206;&#xFF0C;&#x8868;&#x793A;&#x7B49;&#x4E8E;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;</p>
<p>&#x67E5;&#x8BE2;&#x56FE;&#x4E66;&#xFF0C;&#x8981;&#x6C42;&#x56FE;&#x4E66;&#x82F1;&#x96C4;&#x4E3A;&quot;&#x5B59;&#x609F;&#x7A7A;&quot;</p>
<pre><code class="lang-python">BookInfo.objects.filter(heroinfo__hname=<span class="hljs-string">&apos;&#x5B59;&#x609F;&#x7A7A;&apos;</span>)
</code></pre>
<p>&#x67E5;&#x8BE2;&#x56FE;&#x4E66;&#xFF0C;&#x8981;&#x6C42;&#x56FE;&#x4E66;&#x4E2D;&#x82F1;&#x96C4;&#x7684;&#x63CF;&#x8FF0;&#x5305;&#x542B;&quot;&#x516B;&quot;</p>
<pre><code class="lang-python">BookInfo.objects.filter(heroinfo__hcomment__contains=<span class="hljs-string">&apos;&#x516B;&apos;</span>)
</code></pre>
<p><strong>&#x7531;&#x4E00;&#x6A21;&#x578B;&#x7C7B;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x591A;&#x6A21;&#x578B;&#x7C7B;&#x6570;&#x636E;</strong>: </p>
<p>&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>&#x4E00;&#x6A21;&#x578B;&#x7C7B;&#x5173;&#x8054;&#x5C5E;&#x6027;&#x540D;__&#x4E00;&#x6A21;&#x578B;&#x7C7B;&#x5C5E;&#x6027;&#x540D;__&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;=&#x503C;
</code></pre><p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x6CA1;&#x6709;&quot;__&#x8FD0;&#x7B97;&#x7B26;&quot;&#x90E8;&#x5206;&#xFF0C;&#x8868;&#x793A;&#x7B49;&#x4E8E;&#x3002;</strong></p>
<p>&#x4F8B;&#xFF1A;</p>
<p>&#x67E5;&#x8BE2;&#x4E66;&#x540D;&#x4E3A;&#x201C;&#x5929;&#x9F99;&#x516B;&#x90E8;&#x201D;&#x7684;&#x6240;&#x6709;&#x82F1;&#x96C4;&#x3002;</p>
<pre><code class="lang-python">HeroInfo.objects.filter(hbook__btitle=<span class="hljs-string">&apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;</span>)
</code></pre>
<p>&#x67E5;&#x8BE2;&#x56FE;&#x4E66;&#x9605;&#x8BFB;&#x91CF;&#x5927;&#x4E8E;30&#x7684;&#x6240;&#x6709;&#x82F1;&#x96C4;</p>
<pre><code class="lang-python">HeroInfo.objects.filter(hbook__bread__gt=<span class="hljs-number">30</span>)
</code></pre>
<h2 id="3-&#x4FEE;&#x6539;">3 &#x4FEE;&#x6539;</h2>
<p>&#x4FEE;&#x6539;&#x66F4;&#x65B0;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;</p>
<p><strong>1&#xFF09;save</strong></p>
<p><strong>&#x4FEE;&#x6539;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C;save()&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-python">hero = HeroInfo.objects.get(hname=<span class="hljs-string">&apos;&#x732A;&#x516B;&#x6212;&apos;</span>)
hero.hname = <span class="hljs-string">&apos;&#x732A;&#x609F;&#x80FD;&apos;</span>
hero.save()
</code></pre>
<p><strong>2&#xFF09;update</strong></p>
<p><strong>&#x4F7F;&#x7528;&#x6A21;&#x578B;&#x7C7B;.objects.filter().update()</strong>&#xFF0C;&#x4F1A;&#x8FD4;&#x56DE;&#x53D7;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;</p>
<pre><code class="lang-python">HeroInfo.objects.filter(hname=<span class="hljs-string">&apos;&#x6C99;&#x609F;&#x51C0;&apos;</span>).update(hname=<span class="hljs-string">&apos;&#x6C99;&#x50E7;&apos;</span>)
</code></pre>
<h2 id="4-&#x5220;&#x9664;">4 &#x5220;&#x9664;</h2>
<p>&#x5220;&#x9664;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;</p>
<p><strong>1&#xFF09;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;delete</strong></p>
<pre><code class="lang-python">hero = HeroInfo.objects.get(id=<span class="hljs-number">13</span>)
hero.delete()
</code></pre>
<p><strong>2&#xFF09;&#x6A21;&#x578B;&#x7C7B;.objects.filter().delete()</strong></p>
<pre><code class="lang-python">HeroInfo.objects.filter(id=<span class="hljs-number">14</span>).delete()
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C05-Database/Tools.html" class="navigation navigation-prev " aria-label="Previous page: 演示工具使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Database/QuerySet.html" class="navigation navigation-next " aria-label="Next page: 查询集QuerySet"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
